clc;
clear;
% the encode length
stu_num = 2019111816;
code_len = decode_stu_num(stu_num);

% parameters
max_x = 50; % [0,50]
group_size = 60;    % the size of the ethnic group
pc = 0.8;   % the percent of the cross over
pm = 0.007; % the percent of the mutation
max_code = 2^code_len-1;

% initialize the group
new_group = GroupInit(group_size,code_len);
obj_value = cal_objvalue(new_group,max_code,max_x);
fit_value = cal_fitvalue(obj_value);
% max_out = 0;
for i = 1:50
    % update group
    n = Selection(fit_value);
    old_group = new_group;
    new_group = UpdateGroup(old_group,n);
    new_group = CrossOver(new_group, pc);
    new_group = Variation(new_group, pm);
     % calculate object value
    obj_value = cal_objvalue(new_group,max_code,max_x);
    fit_value = cal_fitvalue(obj_value);
end
[M,I] = max(obj_value);
mx = convert2decimal(new_group(I,:))/max_code*max_x;
my = M;
x = convert2decimal(new_group)/max_code*max_x;
y = obj_value;

figure(1)
fplot(@(x)x+5.*cos(3.*x)+6.*sin(4.*x));
hold on
plot(x,y,'*','Color','black');
plot(mx,my,'r*');
axis([0 50 0 60]);
hold off;